#!/usr/bin/env python3
import json, glob, os, datetime
from jinja2 import Template

TMPL = """
<!DOCTYPE html><html lang="zh-CN">
<head><meta charset="utf-8"><title>OpenCloudOS 批量测试报告</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
</head><body class="p-4">
<h1 class="mb-4">测试汇总报告</h1>
<table class="table table-striped table-bordered">
<thead class="table-light"><tr>
  <th>包名</th><th>版本</th><th>安装</th><th>测试</th><th>设备/备注</th>
</tr></thead><tbody>
{% for r in rows %}
<tr>
  <td>{{ r.package }}</td>
  <td>{{ r.version or '-' }}</td>
  <td class="text-{{ 'success' if r.install_success else 'danger' }}">
    {{ '✅' if r.install_success else '❌' }}</td>
  <td class="text-{{ 'success' if r.test_success else 'danger' }}">
    {{ '✅' if r.test_success else '❌' }}</td>
  <td><small>{{ r.device or '-' }}</small></td>
</tr>
{% endfor %}
</tbody></table>
<p class="text-muted">生成时间：{{ ts }}</p>
</body></html>
"""

def main():
    logs = [json.load(open(f, encoding='utf-8')) for f in glob.glob("logs/*.json")]
    rows = []
    for d in logs:
        rows.append({
            "package": d["package"],
            "version": d.get("version"),
            "install_success": d["install_success"],
            "test_success": d["test_success"],
            "device": d.get("device")
        })
    html = Template(TMPL).render(rows=rows, ts=datetime.datetime.now())
    with open("report.html", "w", encoding="utf-8") as f:
        f.write(html)
    print("report.html 已生成")

if __name__ == "__main__":
    main()